$(function () {
    var exam_datagrid = $("#exam_datagrid");
    var exam_dialog = $("#exam_dialog");
    var exam_form = $("#exam_form");
    exam_datagrid.datagrid({
        url: '/exam/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'id', title: '考试编号', width: 100,},
            {field: 'name', title: '姓名', width: 100,},
            {field: 'salerName', title: '营销人员', width: 100},
            {field: 'qq', title: 'QQ', width: 100},
            {field: 'tel', title: '电话', width: 100},
            {field: 'wantClass', title: '意向班级', width: 100},
            {field: 'examTime', title: '考试时间', width: 100},
            {
                field: 'examResult', title: '考试结果', width: 100, formatter: function (value) {
                if (value == 0) {
                    return '<font color="orange">未审核</font>';
                } else if (value == 1) {
                    return '<font color="green">考试通过</font>';
                } else if (value == 2) {
                    return '<font color="red">考试不通过</font>';
                }
            }
            },
            {field: 'remark', title: '备注', width: 100},
            {
                field: 'employeeName', title: '处理人', width: 100
            }
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#exam_btns",
        onSelect: function (index, row) {
            if (row.examResult == 1 || row.examResult == 2) {
                $("#refuse").hide();
                $("#pass").hide();
            } else {
                $("#refuse").show();
                $("#pass").show();
            }
        }
    });
    function formatDate(str) {
        if (str != null) {
            return (new Date(parseInt(str.substring(str.indexOf('(') + 1, str.indexOf(')'))))).format("yyyy-MM-dd");
        }

    }

    $("#import_dialog").dialog({
        title: '文件上传',
        width: 200,
        height: 200,
        closed: true,
        buttons: '#import_btns',
        onClose: function () {
            $("#import_form").form('clear');
        }
    })
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObject[methodName]();
    })
    var methodObject = {
        pass: function () {
            var row = exam_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/exam/pass.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            exam_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        refuse: function () {
            var row = exam_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/exam/refuse.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            exam_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            exam_datagrid.datagrid('load', []);
        },
        cancel: function () {
            exam_dialog.dialog('close');
        },
        query: function () {
            var name = $("#client_id").val();
            $.post('/exam/query.do', {name: name}, function (data) {
                exam_datagrid.datagrid('loadData', data);
            })
        },
        exportXls: function () {
            var keyword = $("[name=keyword]").val();
            window.location.href = '/exam/exportXls.do?keyword=' + keyword;
        },
        import: function () {
            $("#import_dialog").dialog('open');
        },
        import_save: function () {
            $("#import_form").form('submit', {
                url: '/exam/importXls.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '上传成功', 'info');
                        methodObject.import_cancel();
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },
        import_cancel: function () {
            $("#import_dialog").dialog('close');
        }
    }
})


